package com.catsic;

import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;

import com.esri.android.map.Callout;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.tasks.identify.IdentifyParameters;
import com.esri.core.tasks.identify.IdentifyResult;
import com.esri.core.tasks.identify.IdentifyTask;

public class IdentifyTask0Activity  extends Activity{
	
	MapView map;
	GraphicsLayer gl;
	Callout callout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		map = (MapView) findViewById(R.id.map);
		
		map.addLayer(new ArcGISDynamicMapServiceLayer(getResources().getString(R.string.basemap_url)));
		map.addLayer(new ArcGISDynamicMapServiceLayer(getResources().getString(R.string.operationallayer_url)));

		gl = new GraphicsLayer();
		map.addLayer(gl);
		
		
		map.setOnSingleTapListener(new OnSingleTapListener() {

			private static final long serialVersionUID = 1L;
			public void onSingleTap(float x, float y) {

				if (!map.isLoaded())
					return;
				
				IdentifyParameters params = new IdentifyParameters();
				params.setTolerance(20);
				params.setDPI(98);
				params.setLayers(new int[]{0,1,2,3,4,5,6,7,8,9,10,11});
				params.setLayerMode(IdentifyParameters.VISIBLE_LAYERS);
				
				
				Point identifyPoint =  map.toMapPoint(x,y);
				params.setGeometry(identifyPoint);
				params.setSpatialReference(map.getSpatialReference());
				params.setMapHeight(map.getHeight());
				params.setMapWidth(map.getWidth());
				
				Envelope env = new Envelope();
				map.getExtent().queryEnvelope(env);
				params.setMapExtent(env);
				
				MyIdentifyTask mTask = new MyIdentifyTask(identifyPoint);
				mTask.execute(params);
				
			}
		});
		
	}
	
	class MyIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]>{
		
		IdentifyTask identifyTask;
		Point point;
		
		MyIdentifyTask(Point point){
			this.point = point;
		}
		
		@Override
		protected void onPreExecute() {
			identifyTask = new IdentifyTask(getResources().getString(R.string.operationallayer_url));
		}

		@Override
		protected IdentifyResult[] doInBackground(IdentifyParameters... params) {
			IdentifyResult [] result = null;
			if (params!=null && params.length>0) {
				IdentifyParameters  param = params[0];
				try {
					result = identifyTask.execute(param);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(IdentifyResult[] result) {
			Map<String, Object> mapValue = null;
			if (result!=null && result.length >0) {
				IdentifyResult identifyResult = result[0];
				mapValue = result[0].getAttributes();
				
				if (mapValue.containsKey("LXBM")) {
					
					gl.removeAll();
					
					SimpleLineSymbol sls = new SimpleLineSymbol(Color.RED, 25, com.esri.core.symbol.SimpleLineSymbol.STYLE.SOLID);
					
					Graphic nGraphic = new Graphic(identifyResult.getGeometry(),sls,identifyResult.getAttributes());
					gl.addGraphic(nGraphic);
				}
				
			}
			
		}
		
	}
	
}
